package com.gaoyuzhe.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 *
 * @author gaoyuzhe
 */
@RestController
@RequestMapping("/sentinel")
@Slf4j
public class FlowLimitController {

    @GetMapping("/limit1")
    public String limit1() {
        return "limit1"+"haha";
    }
    @GetMapping("/limit2")
    public String limit2() {
        return "limit2"+"haha";
    }

    @GetMapping("/testRT")
    public String testRt() {
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        log.info("testRT");
        return "testRT";
    }

    @GetMapping("/testDegradeGradeExceptionRatio")
    @SentinelResource(value = "testDegradeGradeExceptionRatio")
    public String testDegradeGradeExceptionRatio() {
        //必然的错误
        int i = 1 / 0;
        log.info("testDegradeGradeExceptionRatio");
        return "testDegradeGradeExceptionRatio";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "testHotKeyBlockHandler")
    public String testHotKey(@RequestParam(value = "param1",required = false) String param1,
                             @RequestParam(value = "param2",required = false) String param2) {

      log.info("请求的参数为:p1:{},p2:{}",param1,param2);
        return "testHotKey";
    }
    public String testHotKeyBlockHandler(String param1, String param2, BlockException blockException) {

        log.info("请求的参数为:p1:{},p2:{}",param1,param2);
        return "testHotKeyBlockHandler:"+blockException.getMessage();
    }
}
